<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("logoWrite.htm");
include "setting.php";
include "../include/function.php";

$old_logo_image=$_POST['old_logo_image'];
$logo_name=formatDataForMySql($_POST['logo_name']);
$logo_link=formatDataForMySql($_POST['logo_link']);
$logo_position=intval($_POST['logo_position']);
$logo_isactive=intval($_POST['logo_isactive']);

$ID=intval(_POST('ID'));
$type=$_POST['type'];

//
$date = date("Y-m-d");
$sub_mit = $_POST['sub_mit'];
if($sub_mit=="yes"){
	if($logo_name==""){
		$error_logo_name ="Bạn phải nhập Tên <br />";
	}
	
	//UPLOAD NEW FILE
	if(!$DELETE_ORIGIN_IMAGE)
	{
		if($err=="")
		{
			if($_FILES['logo_image'])
			{
				$logo_image=uploadfile($path, $_FILES['logo_image'],0,$MAX_FILE_SIZE);
			}
		}
		if($logo_image=='-1')
		{
			$error_logo_image.="Lỗi, không thể upload";
			$err="yes";
		}
		else if($logo_image=='0')
		{
			$error_logo_image.="File không đúng kiểu";
			$err="yes";
		}
		else if($logo_image=='1')
		{
			$max_file_size=$MAX_FILE_SIZE/1024;
			$error_logo_image.="Ảnh quá lớn";
			$err="yes";
		}
		else if($logo_image=='2')
		{
			$logo_image="";
		}	
		
		$logo_image_tmp=$logo_image;
	}
	else
	{
		if($_FILES['logo_image']['name'])
		{
			$myfile=str_replace(" ","_",$_FILES['logo_image']['name']);
			$logo_image_tmp=time()."_".$myfile;
			$no_origin=1;
		}
	}
	if($err=="" && $logo_image_tmp!="")
	{
		//Create thumbnail
		if($IS_CREATE_THUMB){
			for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
			{
				$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
				thumbnail_images($_FILES['logo_image'], $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $logo_image_tmp, 0, $text); 
			}
		}
		//if($DELETE_ORIGIN_IMAGE)
//		{
//			deletefile($path.$logo_image_tmp);
//		}
		if(count($MAX_THUMB_WIDTH) && $no_origin==1)
		{
			$logo_image=str_replace(" ","_",$logo_image_tmp);
		}
	}
	
	$str_error=$error_logo_name;
	if($str_error==""){
		if($type=="ADD"){	
			$sql="INSERT INTO ".pre_table."logo(logo_name, logo_link, logo_image, logo_position, logo_isactive, lang) 
					VALUES	('".$logo_name."', '".$logo_link."', '".$logo_image."', '".$logo_position."', '".$logo_isactive."', '".$lang."')";
			execSQL($sql);
		}else{
			//Create thumbnail
			if($IS_CREATE_THUMB && $old_logo_image){
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
					thumbnail_images('', $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $old_logo_image, 0, $text); 
				}
			}
			
		    if($old_logo_image && $logo_image)
			{
				deletefile($path.$old_logo_image);
				//Delete thumbnail
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					deletefile($path."thumb_".$MAX_THUMB_WIDTH[$i]."_".$old_logo_image);
				}
			}
			if(!$logo_image && $old_logo_image)
			{
				$logo_image=$old_logo_image;
			}
			
			$sql="UPDATE ".pre_table."logo SET 
						logo_name='".$logo_name."', 
						logo_link='".$logo_link."', 
						logo_image='".$logo_image."', 
						logo_position='".$logo_position."', 
						logo_isactive='".$logo_isactive."',
						lang='".$lang."'
						WHERE logo_id='".$ID."'";			
			execSQL($sql);
		}			

		$act = $_POST['act'];
		if($act=='save'){
			echo '<script>
				parent.document.frmData.target = "_parent";				
				parent.window.location.href="logo.php";
			</script>';
		}else{	
			echo '<script>
				parent.document.frmData.target = "_parent";	
				parent.document.frmData.logo_name.focus();			
				parent.window.location.href="logoWrite.php";
			</script>';
		}	
		
	}else{
		echo '<script>
				parent.document.frmData.target = "_parent";		
				parent.document.getElementById("err").style.display="block";
				parent.document.getElementById("err_string").innerHTML="'.$str_error.'";
			</script>';	
	}
}
?>